﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using Nyamr.Implementation;

namespace Nyamr
{
    class Nyamr
    {
        public static void Run(Plan plan)
        {
            plan.Validate();

            Directory.CreateDirectory(plan.Target);
            Directory.CreateDirectory(Path.Combine(plan.Target, "data"));

            var assembly = Assembly.LoadFile(plan.Task);
            var taskType = assembly.GetExportedTypes().Where(x => x.Name == plan.TaskName).First();
            var task = Activator.CreateInstance(taskType);
            var mapper = task as BaseMapper;
            if (mapper != null)
            {
                var host = new MapperHost();
                host.Mapper = mapper;
                host.Source = plan.Source;
                host.Target = plan.Target;
                host.Mapper.Settings = plan.Settings;
                host.Execute();
            }
            var reducer = task as BaseReducer;
            if (reducer != null)
            {
                var host = new ReducerHost();
                host.Reducer = reducer;
                host.Source = plan.Source;
                host.Target = plan.Target;
                host.Reducer.Settings = plan.Settings;
                host.Execute();
            }
        }

        public static void GetStatistics(Plan plan)
        {
            var assembly = Assembly.LoadFile(plan.Task);
            var taskType = assembly.GetExportedTypes().Where(x => x.Name == plan.TaskName).First();
            var task = Activator.CreateInstance(taskType);
            var mapper = task as BaseMapper;
            if (mapper != null)
            {
                var host = new MapperHost();
                host.Mapper = mapper;
                host.Source = plan.Source;
                host.Mapper.Settings = plan.Settings;
                host.GetStatistics();
            }
            var reducer = task as BaseReducer;
            if (reducer != null)
            {
                throw new NotImplementedException();
                var host = new ReducerHost();
                host.Reducer = reducer;
                host.Source = plan.Source;
                host.Reducer.Settings = plan.Settings;
                host.Execute();
            }
        }

        public static void Run(string file)
        {
            Plan plan = Plan.Load(file);
            Run(plan);
        }

        public static void Test(Plan plan, string key)
        {
            var assembly = Assembly.LoadFile(plan.Task);
            var taskType = assembly.GetExportedTypes().Where(x => x.Name == plan.TaskName).First();
            var task = Activator.CreateInstance(taskType);
            var mapper = task as BaseMapper;
            if (mapper != null)
            {
                var host = new MapperHost();
                host.Mapper = mapper;
                host.Source = plan.Source;
                host.Target = plan.Target;
                host.Mapper.Settings = plan.Settings;
                host.Execute(key);
            }
            var reducer = task as BaseReducer;
            if (reducer != null)
            {
                var host = new ReducerHost();
                host.Reducer = reducer;
                host.Source = plan.Source;
                host.Target = plan.Target;
                host.Reducer.Settings = plan.Settings;
                host.Execute(key);
            }
        }
    }
}
